﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;

// Pour en savoir plus sur le modèle d'élément Contrôle utilisateur, consultez la page http://go.microsoft.com/fwlink/?LinkId=234236

namespace BlipSansBlop
{
    public enum EtatTir { Vol = 1, Impact = 2, Destroy = 3 };
    public sealed partial class Tir : UserControl
    {

        public EtatTir Etat { get; set; }
        public Point Deplacement { get; set; }
        public Point Position { get; set; }
        public CompositeTransform TirComposite { get; set; }
        public int Degats { get; set; }
        public Tir(Point depart, Double angle)
        {
            this.InitializeComponent();
            double AngleRadian = MainPage.DegreeToRadian(angle);
            int vitesse = 20;
            this.Deplacement = new Point(Math.Cos(AngleRadian) * vitesse, Math.Sin(AngleRadian) * vitesse);
            this.Position = new Point(MainPage.AproxValue((int)depart.X,6), MainPage.AproxValue((int)depart.Y , 6));

            this.Degats = MainPage.rand.Next(30, 60);
            this.Etat = EtatTir.Vol;
            MainPage.Tirs.Children.Add(this);
            this.Draw();
            this.TirComposite = this.LayoutRootComposite;
            this.TirComposite.Rotation = angle + 90;

        }


        public void SwitchToEtat()
        {
            switch (this.Etat)
            {
                case EtatTir.Vol:
                    this.Position = new Point(this.Position.X + this.Deplacement.X, this.Position.Y + this.Deplacement.Y);
                    this.Draw();
                    break;
                case EtatTir.Impact:
                    this.Position = new Point(this.Position.X - this.Deplacement.X, this.Position.Y - this.Deplacement.Y);
                    this.TirImg.Visibility = Visibility.Collapsed;
                    this.TirImpactImg.Visibility = Visibility.Visible;
                    this.Draw();
                    break;
                case EtatTir.Destroy:
                    MainPage.Tirs.Children.Remove(this);
                    break;
            }
            if ((HitTest(MainPage.Obstacles) || HitTest(MainPage.Ennemis)) && this.Etat == EtatTir.Vol) this.Etat = EtatTir.Impact;
            else if (this.Etat == EtatTir.Impact) this.Etat = EtatTir.Destroy;
        }

        private void Draw()
        {
            Canvas.SetLeft(this, Position.X);
            Canvas.SetTop(this, Position.Y);
        }


        public Boolean HitTest(UIElement DossierParent)
        {
            Point test = new Point(this.Position.X + this.Width / 2, this.Position.Y + this.Height / 2);
            IEnumerable<UIElement> List = VisualTreeHelper.FindElementsInHostCoordinates(test, DossierParent);
            
            if (List.Count() != 0) return true;
            return false;
        }
    }
}
